

** set path
// gl path ""

** open log
cap log close
log using "$path/table6.log", replace

use "$path/turnout_analysis.dta", clear

keep voterid p_ g_ foreclose zy year reg_year
gen reg = reg_year <= 2003 | reg_year == . 
drop reg_year

// generate pre-treatment measures of dem and rep
gen dem_pre_treat_temp = p_ == 1 & year == 2000
egen dem_pre_treat = max(dem_pre_treat_temp), by(voterid)
drop dem_pre_treat_temp
gen rep_pre_treat_temp = p_ == 2 & year == 2000
egen rep_pre_treat = max(rep_pre_treat_temp), by(voterid)
drop rep_pre_treat_temp
drop p

gen foreclose_dem = foreclose * dem_pre_treat
gen foreclose_rep = foreclose * rep_pre_treat
drop dem_pre_treat rep_pre_treat

compress

reghdfe g_ foreclose, a(voterid zy) cluster(voterid)
local b1 = _b[foreclose]
local se1 = _se[foreclose]
local n1 = e(N)
local nclust1 = e(N_clust)

reghdfe g_ foreclose, a(voterid##c.year zy) cluster(voterid)
local b2 = _b[foreclose]
local se2 = _se[foreclose]
local n2 = e(N)
local nclust2 = e(N_clust)

reghdfe g_ foreclose foreclose_dem foreclose_rep, a(voterid zy) cluster(voterid)
local b3 = _b[foreclose]
local se3 = _se[foreclose]
local b3_2 = _b[foreclose_dem]
local se3_2 = _se[foreclose_dem]
local b3_3 = _b[foreclose_rep]
local se3_3 = _se[foreclose_rep]
local n3 = e(N)
local nclust3 = e(N_clust)

reghdfe g_ foreclose foreclose_dem foreclose_rep, a(voterid##c.year zy) cluster(voterid)
local b4 = _b[foreclose]
local se4 = _se[foreclose]
local b4_2 = _b[foreclose_dem]
local se4_2 = _se[foreclose_dem]
local b4_3 = _b[foreclose_rep]
local se4_3 = _se[foreclose_rep]
local n4 = e(N)
local nclust4 = e(N_clust)

keep if reg
drop reg

reghdfe g_ foreclose, a(voterid zy) cluster(voterid)
local b5 = _b[foreclose]
local se5 = _se[foreclose]
local n5 = e(N)
local nclust5 = e(N_clust)

reghdfe g_ foreclose, a(voterid##c.year zy) cluster(voterid)
local b6 = _b[foreclose]
local se6 = _se[foreclose]
local n6 = e(N)
local nclust6 = e(N_clust)

reghdfe g_ foreclose foreclose_dem foreclose_rep, a(voterid zy) cluster(voterid)
local b7 = _b[foreclose]
local se7 = _se[foreclose]
local b7_2 = _b[foreclose_dem]
local se7_2 = _se[foreclose_dem]
local b7_3 = _b[foreclose_rep]
local se7_3 = _se[foreclose_rep]
local n7 = e(N)
local nclust7 = e(N_clust)

reghdfe g_ foreclose foreclose_dem foreclose_rep, a(voterid##c.year zy) cluster(voterid)
local b8 = _b[foreclose]
local se8 = _se[foreclose]
local b8_2 = _b[foreclose_dem]
local se8_2 = _se[foreclose_dem]
local b8_3 = _b[foreclose_rep]
local se8_3 = _se[foreclose_rep]
local n8 = e(N)
local nclust8 = e(N_clust)

log close

quietly {
	cap log close
	set linesize 255
	log using "$path/foreclosure_indiv.tex", text replace
		
	noisily dis " \begin{table}[t] "
	noisily dis " \centering "
	noisily dis " \caption{\textbf{Difference-in-Differences Effects of Housing Foreclosures on Individual Turnout in Ohio, 2004 to 2016.} \label{tab:indiv}} "
	noisily dis " \resizebox{\textwidth}{!}{  "
	noisily dis " \begin{tabular}{lcccc} "
	noisily dis " \toprule \toprule "
	noisily dis "  & \multicolumn{4}{c}{Turn Out in General Election (Yes/No)}\\ \midrule "
	noisily dis " & \multicolumn{4}{c}{A. Full Voter File} \\ \cline{2-5}  "
	noisily dis "  & (1) & (2) & (3) & (4) \\ "
	noisily dis " \midrule "
	noisily dis " Home Foreclosed & " %4.3f `b1' " & " %4.3f `b2' " & " %4.3f `b3' " & " %4.3f `b4' " \\ "
	noisily dis "  & (" %4.3f `se1' ") & (" %4.3f `se2' ") & (" %4.3f `se3' ") & (" %4.3f `se4' ")   \\[2mm] "
	noisily dis " Home Foreclosed * Dem Registrant & & & " %4.3f `b3_2' "  & " %4.3f `b4_2' " \\  "
	noisily dis "  & &  & (" %4.3f `se3_2' ") & (" %4.3f `se4_2' ")  \\[2mm] "
	noisily dis " Home Foreclosed * Rep Registrant & & & " %4.3f `b3_3' "  & " %4.3f `b4_3' " \\  "
	noisily dis " & &  & (" %4.3f `se3_2' ") & (" %4.3f `se4_2' ")  \\[2mm] "
	noisily dis "  N &  " %12.0fc `n1' " & " %12.0fc `n2' " & " %12.0fc `n3' " & " %12.0fc `n4' " \\[2mm] \midrule "
	noisily dis "  & \multicolumn{4}{c}{B. Registered Before 2004} \\ \cline{2-5}  "
	noisily dis " Home Foreclosed & " %4.3f `b5' " & " %4.3f `b6' " & " %4.3f `b7' " & " %4.3f `b8' " \\ "
	noisily dis "  & (" %4.3f `se5' ") & (" %4.3f `se6' ") & (" %4.3f `se7' ") & (" %4.3f `se8' ")   \\[2mm] "
	noisily dis " Home Foreclosed * Dem Registrant & & & " %4.3f `b7_2' "  & " %4.3f `b8_2' " \\  "
	noisily dis "  & &  & (" %4.3f `se7_2' ") & (" %4.3f `se8_2' ")  \\[2mm] "
	noisily dis " Home Foreclosed * Rep Registrant & & & " %4.3f `b7_3' "  & " %4.3f `b8_3' " \\  "
	noisily dis " & &  & (" %4.3f `se7_3' ") & (" %4.3f `se8_3' ")  \\[2mm] "
	noisily dis "  N &  " %12.0fc `n5' " & " %12.0fc `n6' " & " %12.0fc `n7' " & " %12.0fc `n8' " \\[2mm] \midrule "
	noisily dis " Individual Fixed Effects & Y & Y & Y & Y   \\ "
	noisily dis " Zip Code-Year Fixed Effects  & Y & Y & Y & Y  \\ "
	noisily dis " Linear Time Trends  & N & Y & N & Y  \\ "
	noisily dis " \bottomrule \bottomrule "
	noisily dis " \multicolumn{5}{p{\textwidth}}{\footnotesize Robust standard errors clustered by individual in parentheses.  "
	noisily dis "Each observation is a person-election year.} "
	noisily dis " \end{tabular}} "
	noisily dis " \end{table} "
	
	log off
}


